package cn.hefan.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.hefan.domain.Cominf;
import cn.hefan.domain.Taolun;
import cn.hefan.domain.Task;
import cn.hefan.domain.User;
import cn.hefan.utils.DataSourceUtils;

public class Dao {

	public static User login(String phone, String password) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from user where username=? and password=?";
		User user = qr.query(sql, new BeanHandler<User>(User.class),phone,password);
		return user;
	}

	public static Cominf selectCompany() throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from corpn";
		Cominf com = qr.query(sql, new BeanHandler<Cominf>(Cominf.class));
		return com;
	}

	public static List<Task> selectTask(String divide) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from task where divide=? and taskstatus=?";
		return qr.query(sql, new BeanListHandler<Task>(Task.class),divide,"未完成");
	}

	public static void jsTask(String username, String id) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql ="update task set taskstatus=?,phone=? where id=?";
		qr.update(sql,"待完成",username,id);
		
	}

	public static List<Task> dwctTask(String username) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from task where phone=? and taskstatus=?";
		return qr.query(sql, new BeanListHandler<Task>(Task.class),username,"待完成");
	}

	public static void wctTask(String username,String name, String phone, String shenfen, String sex, String taskid) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql  = "insert into xtask(name,sex,phone,shenfen,taskid,username) values(?,?,?,?,?,?)";
		qr.update(sql,name,sex,phone,shenfen,taskid,username);

	}

	public static void endTask(String id) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql ="update task set taskstatus=? where id=?";
		qr.update(sql,"待审核",id);
	}

	public static List<Task> eTask(String username) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from task where phone=? and taskstatus=?";
		return qr.query(sql, new BeanListHandler<Task>(Task.class),username,"待审核");
	}

	public static void tuTask(String username, String fileName, String id) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "insert into tutask(username,image) values(?,?)";
		String sql1 = "update task set taskstatus=? where id=?";
		qr.update(sql,username,fileName);
		qr.update(sql1,"待审核",id);
	}

	public static List<Taolun> selectTaolun() throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql ="select * from taotun order by id desc";
		return qr.query(sql,new BeanListHandler<Taolun>(Taolun.class));
	}

	public static void addTaolun(String username, String title, String fileName) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "insert into taotun(name,image,title) values(?,?,?)";
		qr.update(sql,username,fileName,title);
	}

}
